package persistence

import (
	"errors"
	"pushserver/msgqueue"
	"pushserver/serialize"
	"time"
)

var (
	Err_MsgNotFound = errors.New("message not found")
)

type OfflineMsg struct {
	msgqueue.Message
	KeepLiveTime time.Time
}

type OffMsgContainer interface {
	Get(id string) (*OfflineMsg, error)
	GetUserMsg(userId string) ([]*OfflineMsg, error)
	Put(*OfflineMsg)
}

func CreateMsgContainer(t string, params ...interface{}) OffMsgContainer {
	switch t {
	case "redis":
		p := params[0].(*RedisOptions)
		c := RedisContainer{
			serializer: serialize.CreateSerializer("json"),
			option:     p,
		}
		c.try()
		return &c
	default:
		c := MemoryContainer{
			msgMap:     make(map[string]*OfflineMsg, 0),
			userMsgMap: make(map[string][]string, 0),
		}
		c.gc(1000 * 120)
		return &c
	}
}
